/**************************************************************************************
 *  Copyright (c)  2010  Pablo Echeverri.
 *  Permission is granted to copy, distribute and/or modify this document
 *  under the terms of the GNU Free Documentation License, Version 1.3
 *  or any later version published by the Free Software Foundation;
 *  with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 *  A copy of the license is included in the section entitled "GNU
 *  Free Documentation License". 
 **************************************************************************************/
package data;

import java.util.*;
import data.tileables.Fort;

/**************************************************************************************
 * Author(s): Pablo Echeverri
 * holds a hashtable of areabonuses, with x,y as the key in an int[] of size 2
 **************************************************************************************/
public class AreaBonusHash
{
	Hashtable<String,AreaBonus> area_bonuses=new Hashtable<String, AreaBonus>();
	LinkedList<Fort> fortList=new LinkedList<Fort>();
	
	public AreaBonusHash()
	{
		
		
	}
	/**************************************************************************************
	 * Author(s): Pablo Echeverri, Leszek Karcz
	 * add given fort to areaBonus mapped to key int[0],int[1]
	 * int[2] holds the radius
	 **************************************************************************************/
	public void addFort(Fort fort, LinkedList<int[]> list)
	{
		AreaBonus currentAreaBonus;
		list.setToFirst();
			for(int i=0; i<list.getSize();i++)//go through every array in list 
			{
				String key = list.peekCurrent()[0]+" "+list.peekCurrent()[1];				
				currentAreaBonus = this.area_bonuses.get(key);
				if(currentAreaBonus!=null)
				{
					currentAreaBonus.AddFort(fort, list.peekCurrent()[2]);//adds fort to areaBonus			
				}
				else
				{
					currentAreaBonus=new AreaBonus(); //create new areaBonus
					currentAreaBonus.AddFort(fort, list.peekCurrent()[2]);//adds fort to areaBonus
					this.area_bonuses.put(key, currentAreaBonus);
				}
				list.next();
			}
			if(!this.fortList.find(fort))//fort not in hashtable
			{
			this.fortList.addBack(fort);
			}
	}
	/**************************************************************************************
	 * Author(s): Pablo Echeverri, Leszek Karcz
	 * calculates the bonus of the areaBonus mapped to x,y
	 **************************************************************************************/
	public int getBonus(int x, int y)
	{
		String key = x+" "+y;
		
		AreaBonus current = this.area_bonuses.get(key);
		if(current!=null)
		{
			return current.calculateBonus();
		}
		else
		{
			return 0;
		}
	}
	
	/**************************************************************************************
	 * Author(s): Pablo Echeverri
	 * removes fort from all areaBonuses in the hash
	 **************************************************************************************/
	public void removeFort(Fort fort)
	{
		Enumeration<AreaBonus> e=this.area_bonuses.elements();
		AreaBonus current;
		if(this.fortList.find(fort))
		{
			while(e.hasMoreElements())
			{
				current=e.nextElement();
				current.removeFort(fort);
			}
			this.fortList.remove(fort);
		}
	}
	
	
	
}